﻿using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using SAS.Bussiness;
using SAS.Core.Entity;

namespace SAS.WebSite.BaseClasses
{
    /// <summary>
    /// Base class of Web UI page
    /// </summary>
    public abstract class BasePage : Page
    {
        #region Public properties

        /// <summary>
        /// Client IP address
        /// </summary>
        public string Ip { get; set; }

        /// <summary>
        /// Current user name that logged in to system
        /// </summary>
        public string UserName { get; set; }

        public new User User
        {
            get
            {
                if (HttpContext.Current.Session["User"] == null)
                {
                    User = new User();
                }

                return (User)HttpContext.Current.Session["User"];
            }

             set
            {
                HttpContext.Current.Session["User"] = value;
            }
        }
        #endregion

        #region Override methods

        protected override void OnPreInit(EventArgs e)
        {
            Ip = string.Empty;
            HttpContext.Current.Session["SessionEnd"] = DateTime.Now.AddMinutes(25).ToString(CultureInfo.InvariantCulture);
            string httpxfor = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (httpxfor != null)
            {
                if (httpxfor.Trim().Length <= 0)
                {
                    string[] iparray = httpxfor.Split(',');
                    if (iparray.Length > 0)
                    {
                        Ip = iparray[0];
                    }
                }
            }
            if (Ip.Length <= 0)
            {
                Ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            //User must be not null active and Token is not null and empty.
            //if (User != null &&  User.IsActive && !String.IsNullOrEmpty(User.TokenKey))
            //{
            //    UserBO userBO = new UserBO
            //        {
            //            FromIP = Ip,
            //        };
            //    if(!userBO.ValidateToken(User).Result)
            //    {
            //        Response.Redirect("~/Default.aspx");
            //    }
            //}
            //else if (User == null || String.IsNullOrEmpty(User.TokenKey))
            //{
            //    Response.Redirect("~/Default.aspx");
            //}
            base.OnPreInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";
            base.OnLoad(e);
        }

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);

            Title = "Neopost eDocument Delivery"; // set page title
        }
        #endregion

        /// <summary>
        /// Clear all session of website.
        /// </summary>
        public void ClearSession()
        {
            //this.SiteNavigation = null;
            HttpContext.Current.Session["User"] = null;
            HttpContext.Current.Session["Client"] = null;
            HttpContext.Current.Session["Nav"] = null;
            HttpContext.Current.Session["SessionEnd"] = null;
            HttpContext.Current.Session["CurrentFilter"] = null;
            HttpContext.Current.Session["Recent"] = null;
            HttpContext.Current.Session["RecentSearches"] = null;
            HttpContext.Current.Session["Favorites"] = null;
            HttpContext.Current.Session["ClientData"] = null;
        }
    }
}